# Contributor: Oleg Titov <oleg.titov@gmail.com>
# Maintainer: Oleg Titov <oleg.titov@gmail.com>
pkgname=k3s
_pkgver=1.18.6+k3s1
pkgver=${_pkgver/+k3s/.}
pkgrel=0
pkgdesc="Lightweight Kubernetes. 5 less than k8s."
url="https://k3s.io"
arch="all !mips !mips64"
license="Apache-2.0"
options="chmod-clean !check" # No test suite from upstream
depends="cni-plugins conntrack-tools containerd coreutils dbus findutils ipset iptables"
makedepends="go linux-headers"
subpackages="$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/rancher/k3s/archive/v$_pkgver.tar.gz
	k3s.confd
	k3s.initd
	k3s.logrotate
	k3s.modules-load
	"
builddir="$srcdir/src/github.com/rancher/$pkgname"

# secfixes:
#   1.18.6.1-r0:
#     - CVE-2020-8557
#     - CVE-2020-8559
#   1.18.3.1-r0:
#     - CVE-2020-10749
#     - CVE-2020-8555
#   0.8.1-r0:
#     - CVE-2019-9512
#     - CVE-2019-9514
#   0.8.0-r0:
#     - CVE-2019-11247
#     - CVE-2019-11249

prepare() {
	export GOPATH="$srcdir"

	mkdir -p "$(dirname $builddir)"
	mv "$srcdir"/$pkgname-${_pkgver/+/-} "$builddir"/

	default_prepare
}

build() {
	msg2 "Prepare GOPATH and GOBIN..."
	export GOPATH="$srcdir"
	export GOBIN="$GOPATH/bin"

	msg2 "Building k3s..."
	go build -o k3s

	msg2 "Building kubectl..."
	go build -o kubectl ./cmd/kubectl
}

package() {
	install -m755 -D k3s \
		"$pkgdir"/usr/bin/k3s
	install -m755 -D kubectl \
		"$pkgdir"/usr/bin/kubectl

	install -m644 -D -t "$pkgdir"/usr/share/doc/$pkgname README.md

	install -m755 -D "$srcdir"/k3s.initd "$pkgdir"/etc/init.d/k3s
	install -m644 -D "$srcdir"/k3s.confd "$pkgdir"/etc/conf.d/k3s
	install -m644 -D "$srcdir"/k3s.logrotate "$pkgdir"/etc/logrotate.d/k3s
	install -m644 -D "$srcdir"/k3s.modules-load "$pkgdir"/etc/modules-load.d/k3s.conf
}

sha512sums="ec7ac86383959327e04fece6f2f390e48ec3d604e23bb738bd886ffc7b02489809f918f7eb0647c492ef90fa4751bdbce578931c6f19d7770909e1cf2be9c45a  k3s-1.18.6.1.tar.gz
134ff1f61c3dda3f5085387869db2873ad5e9a17c1080a3600d37ff48058e79cbcbea3df051c903c1d2e3ff0756dbfa54aa9ec4274b8c91db25912ff64e5357e  k3s.confd
cff8db85337dd6b8a5126ea2c498fec09bc11cfdb856069ca78218cb5e58ce0c4c42a239e1d26bac9893f64ecc81fbdff64f7f937a960475b6298caafca0b36d  k3s.initd
018a5e9b417a937c17f0a4a9e08eed434f06186207626ad038aec22ee667aba4cefa6e9e2a222e2c430d2cbb88c8663648f5bab0e76926a0edd13b8bdfd2673a  k3s.logrotate
85ee1310cb36c85c42b4068a9549a3ef72b856cd61b2c1036c3e871ef43a69ed80b43599ad94ce5b069ddd823e730596bb3d3875d4ba8cd77c4cc1985335ffff  k3s.modules-load"
